<?php
defined ( 'PG_PAGE' ) or die ();

define('PG_GOLD_TYPE_MAIN', 'user_gold');
define('PG_GOLD_TYPE_HOLD', 'user_goldhold');
define('PG_GOLD_TYPE_BLOCK', 'user_goldblock');

class PGGold {
  
  var $user_obj;
  
  var $user_info;
  
  var $user_exists;
  
  var $is_error;
  
  function __construct($userObj){
    if (!is_object($userObj)) return false;
    
    $this->user_obj = $userObj;
    $this->user_info = $this->user_obj->user_info;
    $this->user_exists = $this->user_obj->user_exists;
      
    if (!$this->user_exists){
      $this->is_error = 'Thành viên này không tồn tại';
      return false;
    }
    return true;
  }  
  
  /**
   * Cộng trừ Gold cho thành viên
   * @param int $gold
   * @param boolean $add
   * @param string $gold_type (goldmain, goldhold, goldblock)
   * @return boolean
   */
  public function gold_change($transaction_type, $transaction_id, $gold=0, $note=""){
    global $database, $user;
    
    $gold_type = 'user_gold';
    
    $gold = intval($gold);
    $gold_before = intval($this->user_info[$gold_type]);
    
    if ($this->gold_validate($gold, $gold_type)===false) return false;
    
    $gold_after = $gold + $gold_before;    
    $gold_after_hash = $this->gold_create_hash($gold_after);
    
    $res = $database->db_query("INSERT INTO loggolds(loggold_user_id, loggold_user_email, loggold_action, loggold_action_id, loggold_ip, loggold_created, loggold_before, loggold_after, loggold_referer_id, loggold_note) VALUES (%d, '%s', '%s', %d, '%s', %d, %d, %d, %d, '%s')", $this->user_obj->user_info['user_id'], $this->user_obj->user_info['user_email'], $transaction_type, $transaction_id, $_SERVER['REMOTE_ADDR'], time(), $gold_before, $gold_after, $user->user_info['user_id'], $note);
    
    if ($res){    
      $update_gold = $this->user_obj->user_update_gold($gold_after, $gold_after_hash, $gold_type);
      
      if ($update_gold){
        $this->user_info[$gold_type] = $gold_after;
        $this->user_info[$gold_type.'_hash'] = $gold_after_hash;
    
        return $gold_after;
      }
    }
    
    return false;
  }
  
  /**
   * Kiểm tra thông tin gold
   * @param int $gold
   * @param boolean $add
   * @param string $gold_type (goldmain, goldhold, goldblock)
   * @return boolean
   */
  public function gold_validate($gold=0, $gold_type=PG_GOLD_TYPE_MAIN){
    if (!$this->user_exists){
      $this->is_error = 'Thành viên này không tồn tại';
      return false;
    }
    
    // Check Gold Hash
    if (!$this->gold_check_hash($this->user_info[$gold_type], $this->user_info[$gold_type.'_hash'])){
      $this->is_error = 'Gold Hash không đúng';
      return false;
    }
    
    $gold_before = intval($this->user_info[$gold_type]);
    
    if ($gold_before+$gold<0){
      $this->is_error = 'Số Gold trừ lớn hơn số Gold hiện tại của thành viên';
      return false;
    }
    return true;
  }
  
  /**
   * Kiểm tra tính đúng đắn của Gold Hash
   * @param int $user_gold
   * @param string $user_gold_hash
   * @return boolean
   */
  private function gold_check_hash($user_gold, $user_gold_hash) {
    if ($user_gold==0 && $user_gold_hash=='') return true;
    if (!$this->user_exists){
      $this->is_error = 'Thành viên này không tồn tại';
      return false;
    }
    
    $gold_hash_check = self::gold_create_hash ( $user_gold );
    if ($gold_hash_check === $user_gold_hash)
      return true;
    return false;
  }
  
  /**
   * Tạo mã hoá Gold
   * @param int $user_gold
   * @return string
   */
  private function gold_create_hash($user_gold) {
    if (!$this->user_exists){
      $this->is_error = 'Thành viên này không tồn tại';
      return false;
    }
    
    $gold_hash = md5 ( $this->user_info['user_id'] . $this->user_info['user_email'] . 'shpO.o' . $user_gold );
    return $gold_hash;
  }
}
?>